import os

from langchain_bailian import Bailian
from langchain_community.embeddings import QianfanEmbeddingsEndpoint
from langchain_openai import AzureChatOpenAI, OpenAIEmbeddings, AzureOpenAIEmbeddings, ChatOpenAI, OpenAI
from langchain_wenxin import ChatWenxin

from langchain_wenxin.embeddings import WenxinEmbeddings

baidu_api_key = "IcduHfGXD4ICn744Iq8TFdPY"
baidu_secret_key = "GUhLrQDSQ4L1cBHXz0q37QMH0svOAU9f"


def getLLM(platform='openai', model=''):
    if platform == 'openai':
        return azure_chat_openai(model)
    elif platform == 'qianfan':
        return get_qianfan_wenxin_llm(model)


def azure_chat_openai(model="openai-35"):
    os.environ["OPENAI_API_TYPE"] = "azure"
    os.environ["AZURE_OPENAI_ENDPOINT"] = "https://openai-service-japan.openai.azure.com/"
    os.environ["OPENAI_API_KEY"] = "1384ce09ca044ddaa597a1663dd4fe12"
    os.environ["OPENAI_API_VERSION"] = "2023-07-01-preview"

    if model == '':
        model = 'openai-35'

    llm = AzureChatOpenAI(
        # openai_api_type="azure",
        # # openai_api_base="https://openai-service-japan.openai.azure.com/",
        # openai_api_key="1384ce09ca044ddaa597a1663dd4fe12",
        # openai_api_version="2023-07-01-preview",
        deployment_name=model  # openai-gpt4 openai-35
    )
    return llm


def get_qianfan_wenxin_llm(model="ernie_bot_8k"):
    """
    文心模型：
    ERNIE-Bot：completions
    ERNIE-Bot-8k：ernie_bot_8k
    ERNIE-Bot 4.0：completions_pro
    ERNIE-Bot-turbo: eb-instant
    :param model:
    :return:
    """
    if model == '':
        model = 'ernie_bot_8k'

    llm = ChatWenxin(model=model, baidu_api_key=baidu_api_key,
                     baidu_secret_key=baidu_secret_key)
    return llm


def get_kimi_llm():
    llm = ChatOpenAI(
        openai_api_base="https://api.moonshot.cn/v1/",
        openai_api_key="sk-L33dtu9oODHlq0wnqPS9QL0BYVNdDZs7OB5hnk7gYMdl9lSR",
        model_name="moonshot-v1-8k",
    )
    return llm


def getAzureOpenaiEmbedings():
    os.environ["OPENAI_API_TYPE"] = "azure"
    os.environ["AZURE_OPENAI_ENDPOINT"] = "https://openai-service-japan.openai.azure.com/"
    os.environ["OPENAI_API_KEY"] = "1384ce09ca044ddaa597a1663dd4fe12"
    os.environ["OPENAI_API_VERSION"] = "2023-07-01-preview"
    embeddings = AzureOpenAIEmbeddings(deployment='embedding_002')
    return embeddings


def getQianfanWenxinEmbedings(model="embedding-v1"):
    """
    获取千帆文心的嵌入模型
    :param model:
    :return:
    """
    embeddings = WenxinEmbeddings(model=model, baidu_api_key=baidu_api_key,
                                  baidu_secret_key=baidu_secret_key, truncate="END")
    return embeddings


def getQianfanEmbeddingsEndpoint():
    """
    获取千帆文心的嵌入模型
    :param model:
    :return:
    """
    embeddings = QianfanEmbeddingsEndpoint(qianfan_ak="your qianfan ak",
                                           qianfan_sk="your qianfan sk", )
    return embeddings


def get_ali_bailian_llm(model_name="qwen-max"):
    """
    阿里百练
    :param model_name:
    :return:
    """
    llm = ChatOpenAI(
        openai_api_base="https://dashscope.aliyuncs.com/compatible-mode/v1",
        openai_api_key="sk-f7d5174015b34ada8f4717b8263638fa",
        model_name="qwen-max"
    )
    return llm


def get_xunfei_xinghuo_llm(model_name="generalv3.5"):
    """
    阿里百练
    :param model_name:
    :return:
    """
    llm = ChatOpenAI(
        openai_api_base="https://spark-api-open.xf-yun.com/v1",
        openai_api_key="key123456:secret123456",
        model_name=model_name
    )
    return llm

def get_huoshan_doubao_llm(model_name="ep-20240802111318-mtb79"):
    """
    豆包
    :param model_name:
    :return:
    """
    llm = ChatOpenAI(
        openai_api_base="https://ark.cn-beijing.volces.com/api/v3",
        openai_api_key="6c5069b1-6827-4cf3-a6e2-c504660ff1ef",
        model_name=model_name
    )
    return llm

def get_wantong_gpt4(model_name="ep-20240802111318-mtb79"):
    """
    豆包
    :param model_name:
    :return:
    """
    llm = ChatOpenAI(
        openai_api_base="https://api.ai-gaochao.cn/v1",
        openai_api_key="sk-XYg12wuT0f5EVhsF7780B28d06Fb4053A209970cDc146598"
    )
    return llm

if __name__ == '__main__':
    llm = get_wantong_gpt4()
    print(llm.invoke("how can langsmith help with testing?"))
